package cn.itcast.order.service;

import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;


    public Order queryOrderById(Long orderId) {
        // 1.查询订单   订单和订单所属的user_id
        Order order = orderMapper.findById(orderId);

        //2.获取user_id   请求http://localhost:8081/user/{user_id}   调用了user-service    远程调用  ->User
        //String url = "http://需要远程调用的服务的名字/user/"+order.getUserId();
        String url = "http://userservice/user/"+order.getUserId();
        User user = restTemplate.getForObject(url, User.class);

        //3.将获取的User封装到order.user属性中
        order.setUser(user);

        // 4.返回
        return order;
    }


    public Order queryOrderByIdUrl(Long orderId) {
        // 1.查询订单   订单和订单所属的user_id
        Order order = orderMapper.findById(orderId);

        //2.获取user_id   请求http://localhost:8081/user/{user_id}   调用了user-service    远程调用  ->User
        String url = "http://localhost:8081/user/"+order.getUserId();
        User user = restTemplate.getForObject(url, User.class);

        //3.将获取的User封装到order.user属性中
        order.setUser(user);

        // 4.返回
        return order;
    }
}
